<?php 
class ControllerDesignLayout extends Controller {
	private $error = array();
 
	public function index() {
		$this->load->language('design/layout');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/layout');
		$this->getList();
	}

	public function insert() {
		$this->load->language('design/layout');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/layout');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_design_layout->addLayout($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_insert();
	}

	public function update() {
		$this->load->language('design/layout');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/layout');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_design_layout->editLayout($this->request->get['layout_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_update();
	}
 
	private function getList() {
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('design/layout/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$results = $this->model_design_layout->getLayouts();
		$this->data['layouts'] = array();
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('design/layout/update', 'token=' . $this->session->data['token'] . '&layout_id=' . $result['layout_id'], 'SSL')
			);
			$this->data['layouts'][] = array(
				'layout_id' => $result['layout_id'],
				'name'      => $result['name'],
				'route'     => $result['route'],
				'status'    => $result['status'] == 1 ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
				'action'    => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_route'] = $this->language->get('column_route');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');	

		$this->data['button_insert'] = $this->language->get('button_insert');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->template = 'design/layout_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm_text() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_all'] = $this->language->get('text_all');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_route'] = $this->language->get('entry_route');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
				
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
	}

	private function getForm_insert() {
		$this->data['action'] = $this->url->link('design/layout/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['name'] = '';
		$this->data['route'] = '';
		$this->data['status'] = 0;
				
		$this->template = 'design/layout_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm_update() {
		$this->data['action'] = $this->url->link('design/layout/update', 'token=' . $this->session->data['token'] . '&layout_id=' . $this->request->get['layout_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('design/layout', 'token=' . $this->session->data['token'], 'SSL');
		
		$layout_info = $this->model_design_layout->getLayout($this->request->get['layout_id']);
		$this->data['name'] = $layout_info['name'];
		$this->data['route'] = $layout_info['route'];
		$this->data['status'] = $layout_info['status'];
				
		$this->template = 'design/layout_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>